package benchmarking;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class benchmarker {
	//Instance variables
	private long startTime;
	private long elapsedTime;
	private boolean isRunning=false;
	private WritableSheet sheet;
	private WritableWorkbook workbook;
	
	//constructors
	public benchmarker(){
	}
	
	//Methods
	public void start()
	{
		if (isRunning) return;
		isRunning = true;
		System.gc();
		startTime=System.nanoTime();
	}
	
	public void end()
	{
		if (isRunning)
		{
			elapsedTime = System.nanoTime()-startTime;
			isRunning=false;
		}
		return;
	}
	public String toString()
	{
		return Long.toString(elapsedTime);
	}
	
	public void createSheet(String filename) throws IOException, JXLException
	{
		workbook = Workbook.createWorkbook(new File("Benchmarks/"+filename+".xls"));
		sheet = workbook.createSheet("Benchmark", 0);
	}
	
	public void toExel(String filename) throws JXLException, IOException
	{
		sheet =workbook.getSheet("Benchmark");	
		try {
			if(sheet.getCell(0, 0).getType()==CellType.EMPTY)
			{
			Label label = new Label(0, 0, "Benchmarking "+filename);
			sheet.addCell(label);
			}
			int i=1;
			while(sheet.getCell(0, i).getType()!=CellType.EMPTY)i++;
			Number testnum = new Number(0,i,i);
			sheet.addCell(testnum);
			Number resul= new Number(1,i,elapsedTime);
			sheet.addCell(resul);
			

		} catch (WriteException e) {
			e.printStackTrace();
		} 
	//	catch (IOException e) {
	//		e.printStackTrace();
	//	}
			
	}
	
	public void close() throws WriteException, IOException
	{
		workbook.write();
		workbook.close();
	}
	

	
}
